import { _decorator, Component, UITransform, Prefab, instantiate, Vec3, NodeEventType, Sprite, Color, BaseNode, Label, Animation, AudioSource, AudioClip, sys, director } from 'cc';
import { Index } from '../Index/Index';
import Leves from '../../Data/Index/LevelS';
import { Data, Size, WH } from '../Types';
const { ccclass, property } = _decorator;
@ccclass('BG1')
export class BG1 extends Component {
    static color: Color;
    level: number = 1;
    wh: WH;//父元素的宽高
    life: number = 3;
    @property(Prefab)
    item: Prefab;//默认的子元素预制资源
    @property(Label)
    levelTxt: Label;//关卡文本
    @property(Label)
    levelTxt2: Label;
    @property(Label)
    lifeTxt: Label;//生命文本
    @property(AudioClip)
    music1: AudioClip;
    @property(AudioClip)
    music2: AudioClip;
    start() {
        this.wh = this.node.getComponent(UITransform).contentSize;
        this.init(this.wh, { Xsize: Index.Xsize, Ysize: Index.Ysize });
    }

    update(deltaTime: number) {
        this.levelTxt.string = Index.level.toString();
        this.levelTxt2.string = this.level.toString();
        this.lifeTxt.string = this.life.toString();
    }

    init(wh: WH, size: Size) {
        let width: number = (wh.width / size.Xsize);
        let height: number = (wh.height / size.Ysize);
        let color: Color = this.randomColor();
        let id: number = 0;
        for (let y = 0; y < Index.Ysize; y++) {
            for (let x = 0; x < Index.Xsize; x++) {
                let node = instantiate(this.item);
                node.children[0].getComponent(Sprite).color = color;
                node.getComponent(UITransform).contentSize.set(width, height);//设置宽高
                node.setPosition(new Vec3((x * width), y * height));//设置位置
                // node.on(NodeEventType.MOUSE_DOWN, this.itemClick, this, node);//添加点击事件
                node.on(NodeEventType.TOUCH_START, this.itemClick, this, node);
                node.name = `item${id++}`;
                this.node.addChild(node);
            }
        }
        this.randomSetColor(color);
    }
    //随机返回颜色
    randomColor(): Color {
        let arr: [number, number, number, number] = [0, 0, 0, 0];
        for (let i = 0; i < 4; i++) {
            arr[i] = (Math.random() * 255);
            if (i === 3) {
                arr[3] = 255;
            }
        }
        return new Color(...arr);
    }
    //随机设置一个元素颜色
    randomSetColor(color: Color) {
        let { r, g, b } = color;
        color = new Color(r, g - 100, b);
        let i: number = Math.floor(Math.random() * (Index.Xsize * Index.Ysize));
        this.node.children[i].children[0].getComponent(Sprite).color = color;
        BG1.color = color; //全局设置标记
    }
    //元素点击事件
    itemClick({ target }) {
        let { children }: BaseNode = target;
        if (this.life >= 1) {
            if (children[0].getComponent(Sprite).color._val == BG1.color._val) {
                if (this.level < 10) {
                    this.level += 1;
                } else {
                    if (Index.level <= Leves.length) {
                        Index.Xsize += 1;
                        Index.Ysize += 1;
                        Index.level += 1;
                        this.level = 1;
                        this.save();
                    } else {
                        director.loadScene('Index')
                    }
                }
                this.playMusic(this.music1);
                this.node.removeAllChildren();//清空node
                this.init(this.wh, { Xsize: Index.Xsize, Ysize: Index.Ysize });
            } else {
                this.playMusic(this.music2);
                this.life -= 1;
                this.lifeTxt.getComponent(Animation).play();
                if (this.life <= 0) {
                    this.save();
                    director.loadScene('Index');
                }
            }
        } else {
            this.playMusic(this.music2)
            this.lifeTxt.string = `0`;
            console.log('you 没命了');
        }
    }
    save() {
        let data: Data = JSON.parse(sys.localStorage.getItem('userData'));
        if (data) {
            if (data.level < Index.level) {
                sys.localStorage.setItem('userData', JSON.stringify({ 'level': Index.level }));
            }
        } else {
            sys.localStorage.setItem('userData', JSON.stringify({ 'level': Index.level }));
        }
        Index.levels = false;
    }
    playMusic(file: AudioClip) {
        file.stop();
        file.play();
    }
}
